/// <reference path="jquery-1.10.1.min.js" />
/// <reference path="ProtoClass.js" />
/// <reference path="jquery-ui-1.10.3.custom.min.js" />

function createTwitterButton() {
    var button = $("iframe").first();
    button.remove();
    button = $(".twitter-share-button").first().remove();
    var newButton = $('<a href="https://twitter.com/share" class="twitter-share-button" data-size="large">Tweet</a>');
    var newTextToTweet = "";
    var elements = $("#container-selected > div div");
    elements.each(function () {
        var newTextContent = newTextToTweet + " " + $(this).text();
        if (newTextContent.length <= 140) {
            newTextToTweet = newTextContent;
        }
    });

    newButton.attr("data-text", newTextToTweet);
    $(document.body).append(newButton);
    twttr.widgets.load();
    //(function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } })(document, 'script', 'twitter-wjs');
}


var localStorageControl = (function () {
    var initial = 0;
    var max = -1;
    var current = 0;

    function clickPrevious() {
        if (max == -1) {
            return;
        }

        current = current - 1;
        if (current < initial) {
            current = max;
        }
        
        var objectToRetrieve = localStorage.getItem(current);
        $("#container-selected").html(objectToRetrieve);
        createTwitterButton();
    }

    function clickNext() {
        if (max == -1) {
            return;
        }

        current = current + 1;
        if (current > max) {
            current = initial;
        }

        var objectToRetrieve = localStorage.getItem(current);
        $("#container-selected").first().html(objectToRetrieve);
        createTwitterButton();
    }

    function addElement() {
        var objectToStore = $("#container-selected").first().html();
        max = max + 1;
        localStorage.setItem(max, objectToStore);
        current = max;
    }

    return {
        previous: clickPrevious,
        next: clickNext,
        add: addElement
    }
}());

var Worker = Class.create({
    initialize: function (firstName, lastName, age, workExperience) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.workExperience = workExperience;
    },

    fullName: function () {
        return this.firstName + " " + this.lastName;
    },

    renderPartial: function () {
        var container = $("<div>" + this.fullName() + "</div>");
        container.addClass("worker");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        return container;
    },

    renderFull: function () {
        var container = $("<div></div>");
        container.addClass("worker-full");
        var nameVisual = $("<div>" + this.fullName() + "</div>");
        nameVisual.addClass("name-visualisation");
        container.append(nameVisual);
        var characteristics = $("<div> Age:" + this.age + ", work experience: " + this.workExperience + "</div>");
        characteristics.addClass("characteristics-visualisation");
        container.append(characteristics);

        return container;
    }
});

var Expert = Class.create(Worker, {
    initialize: function ($super, firstName, lastName, age, workExperience, degree) {
        $super(firstName, lastName, age, workExperience);
        this.degree = degree;
    },

    renderPartial: function ($super) {
        var container = $super();
        container.addClass("expert");

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("expert-full");
        var degree = $("<div> Degree:" + this.degree + "</div>");
        degree.addClass("degree-visualisation");
        container.append(degree);

        return container;
    }
});

var Manager = Class.create(Expert, {
    initialize: function ($super, firstName, lastName, age, workExperience, degree) {
        $super(firstName, lastName, age, workExperience, degree);
        this.workers = [];
    },

    addManagedEmployee: function (employee) {
        if (employee instanceof Manager) {
            throw new Error("Managers cannot manage other managers.");
        }

        if (!(employee instanceof Worker)) {
            throw new Error("Only workers can be employees of the manager.");
        }

        for (var i = this.workers.length - 1; i >= 0; i--) {
            if (this.workers[i] === employee) {
                return;
            }
        }

        this.workers.push(employee);
    },

    renderPartial: function ($super) {
        var container = $super();
        container.addClass("manager");

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("manager-full");
        var managedPeople = $("<div> Number of managed employees: " + this.workers.length + "</div>");
        container.append(managedPeople);

        return container;
    }
});

var Building = Class.create({
    initialize: function (name, area, retailPrice, rented, yearlyRent) {
        this.name = name;
        this.area = area;
        this.retailPrice = retailPrice;
        this.rented = rented;
        this.workers = [];
        if (this.rented) {
            this.yearlyRent = yearlyRent;
        }
        else {
            this.yearlyRent = "N/a";
        }
    },

    renderPartial: function () {
        var container = $("<div></div>");
        container.addClass("building");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        var buildingName = $("<div>Building name: " + this.name + "</div>");
        buildingName.addClass("buildingName");

        container.append(buildingName);

        this.container = container;
        return container;
    },

    renderFull: function () {
        var container = $("<div></div>");
        container.addClass("building-full");

        var buildingName = $("<div>Building name: " + this.name + "</div>");
        buildingName.addClass("building-name");
        container.append(buildingName);

        var areaOfBuilding = $("<div>Area of building: " + this.name + " square meters.</div>");
        container.append(areaOfBuilding);

        var retailPriceOfBuilding = $("<div>Retail price:" + this.name + " leva</div>");
        container.append(retailPriceOfBuilding);

        var rentedBuilding;
        if (!this.rented){
            rentedBuilding = $("<div>Building owned</div>");
        }
        else{
            rentedBuilding = $("<div>Building rented for " + this.yearlyRent + " leva per year.</div>");
        }

        container.append(rentedBuilding);

        this.fullContainer = container;
        return container;
    }
});

var AdministrativeBuilding = Class.create(Building, {
    initialize: function ($super, name, area, retailPrice, rented, yearlyRent) {
        $super(name, area, retailPrice, rented, yearlyRent);
        this.workers = [];
    },

    addWorker: function (employee) {
        if (!(employee instanceof Worker)) {
            throw new Error("Only workers can populate buildings.");
        }

        this.workers.push(employee);
        if (this.container) {
            if (this.container.children("ul.list-of-workers").length > 0) {
                $(this.container).children("ul.list-of-workers").first().append(employee.renderPartial());
            }
            else {
                var workersList = $("<ul></ul>");
                workersList.addClass("list-of-workers");
                for (var i = 0, len = this.workers.length; i < len; i++) {
                    var listItem = this.workers[i].renderPartial();
                    workersList.append(listItem);
                }

                workersList.sortable();
                if (this.container.children("ul.list-of-machines").length > 0) {
                    var collection = $(this.container).children(".buildingName");
                    collection.after(workersList);
                }
                else {
                    $(this.container).append(workersList);
                }
            }
        }

        if (this.fullContainer) {
            if ($(this.fullContainer).parent().length > 0) {
                $(this.fullContainer).children(".number-container-workers").first().html("Number of employees in building: " + this.workers.length);
            }
        }
    },

    renderPartial: function ($super) {
        var container = $super();
        container.addClass("administrative");

        if (this.workers.length > 0) {
            var workersList = $("<ul></ul>");
            workersList.addClass("list-of-workers");
            for (var i = 0, len = this.workers.length; i < len; i++) {
                var listItem = this.workers[i].renderPartial();
                workersList.append(listItem);
            }

            workersList.sortable();
            container.append(workersList);
        }

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("administrative-full");

        var managedPeople = $("<div>Number of employees in building: " + this.workers.length + "</div>");
        managedPeople.addClass("number-container-workers");
        container.append(managedPeople);
        container.sortable();

        return container;
    }
});

var ProductionBuilding = Class.create(AdministrativeBuilding, {
    initialize: function ($super, name, area, retailPrice, rented, yearlyRent) {
        $super(name, area, retailPrice, rented, yearlyRent);
        this.machines = [];
        this.vehicles = [];
    },

    addMachine: function (machine) {
        if (!(machine instanceof Machine)) {
            throw new Error("Only machines can be added via this method.");
        }

        this.machines.push(machine);
        if (this.container) {
            if (this.container.children("ul.list-of-machines").length > 0) {
                $(this.container).children("ul.list-of-machines").first().append(machine.renderPartial());
            }
            else {
                var machinesList = $("<ul></ul>");
                machinesList.addClass("list-of-machines");
                for (var i = 0, len = this.machines.length; i < len; i++) {
                    var listItem = this.machines[i].renderPartial();
                    machinesList.append(listItem);
                }

                machinesList.sortable();
                $(this.container).append(machinesList);
            }
        }

        if (this.fullContainer) {
            if ($(this.fullContainer).parent().length > 0) {
                $(this.fullContainer).children(".number-container-machines").first().html("Number of machines in building: " + this.machines.length);
            }
        }
    },

    addVehicle: function (vehicle) {
        if (!(vehicle instanceof Vehicle)) {
            throw new Error("Only vehicles can be added via this method.");
        }

        this.vehicles.push(vehicle);
        if (this.container) {
            if (this.container.children("ul.list-of-machines").length > 0) {
                $(this.container).children("ul.list-of-vehicles").first().append(vehicle.renderPartial());
            }
            else {
                var vehiclesList = $("<ul></ul>");
                vehiclesList.addClass("list-of-vehicles");
                for (var i = 0, len = this.vehicles.length; i < len; i++) {
                    var listItem = this.vehicles[i].renderPartial();
                    vehiclesList.append(listItem);
                }

                vehiclesList.sortable();
                $(this.container).append(vehiclesList);
            }
        }

        if (this.fullContainer) {
            if ($(this.fullContainer).parent().length > 0) {
                $(this.fullContainer).children(".number-container-vehicles").first().html("Number of vehicles in production building: " + this.vehicles.length);
            }
        }
    },

    renderPartial: function ($super) {
        var container = $super();
        container.addClass("production");

        if (this.machines.length > 0) {
            var machinesList = $("<ul></ul>");
            machinesList.addClass("list-of-machines");
            for (var i = 0, len = this.machines.length; i < len; i++) {
                var listItem = this.machines[i].renderPartial();
                machinesList.append(listItem);
            }

            machinesList.sortable();
            container.append(machinesList);
        }

        if (this.vehicles.length > 0) {
            var vehiclesList = $("<ul></ul>");
            vehiclesList.addClass("list-of-vehicles");
            for (var i = 0, len = this.vehicles.length; i < len; i++) {
                var listItem = this.vehicles[i].renderPartial();
                vehiclesList.append(listItem);
            }

            vehiclesList.sortable();
            container.append(vehiclesList);
        }

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("production-full");

        var managedMachines = $("<div>Number of machines in building: " + this.machines.length + "</div>");
        managedMachines.addClass("number-container-machines");
        container.append(managedMachines);
        container.sortable();

        var managedVehicles = $("<div>Number of vehicles in building: " + this.vehicles.length + "</div>");
        managedVehicles.addClass("number-container-vehicles");
        container.append(managedVehicles);
        container.sortable();

        return container;
    }
});

var Machine = Class.create({
    initialize: function (years, guarantee, amortizationPeriod) {
        this.name = "Production machine";
        this.yearsOld = years;
        this.guarantee = guarantee;
        this.amortizationPeriod = amortizationPeriod;
    },

    renderPartial: function () {
        var container = $("<div>" + this.name + "</div>");
        container.addClass("machine");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        return container;
    },

    renderFull: function () {
        var container = $("<div></div>");
        container.addClass("machine-full");

        var nameVisual = $("<div>" + this.name + "</div>");
        nameVisual.addClass("name-visualisation");
        container.append(nameVisual);
        
        var ageVisual = $("<div>Purchased before: " + this.yearsOld + " years</div>");
        ageVisual.addClass("age-visualisation");
        container.append(ageVisual);

        var guaranteeVisual = $("<div>Guarantee left: " + Math.max((this.guarantee - this.yearsOld), 0) + " years</div>");
        guaranteeVisual.addClass("guarantee-visualisation");
        container.append(guaranteeVisual);

        var amortizationVisual = $("<div>Amortization left: " + Math.max((this. amortizationPeriod - this.yearsOld), 0) + " years</div>");
        amortizationVisual.addClass("amortization-visualisation");
        container.append(amortizationVisual);

        return container;
    }
});

var Server = Class.create(Machine, {
    initialize: function ($super, years, guarantee, amortizationPeriod, cores, OS, usage) {
        $super(years, guarantee, amortizationPeriod);
        this.name = "Server";
        this.cores = cores;
        this.OS = OS;
        this.usage = usage;
    },

    renderPartial: function () {
        var container = $("<div>" + this.name + "</div>");
        container.addClass("server");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("server-full");

        var numberCores = $("<div>Number of cores: " + this.cores + "</div>");
        numberCores.addClass("cores-visualisation");
        container.append(numberCores);
        
        var opSystem = $("<div>OS: " + this.OS + "</div>");
        opSystem.addClass("os-visualisation");
        container.append(opSystem);
        
        var purpose = $("<div>Used for: " + this.usage + "</div>");
        purpose.addClass("usage-visualisation");
        container.append(purpose);
        
        return container;
    }
});

var Vehicle = Class.create(Machine, {
    initialize: function ($super, years, guarantee, amortizationPeriod, mileage, leasing, typeOfVehicle) {
        $super(years, guarantee, amortizationPeriod);
        this.name = "Vehicle";
        this.mileage = mileage;
        this.leasing = leasing;
        this.typeOfVehicle = typeOfVehicle;
    },

    renderPartial: function () {
        var container = $("<div>" + this.name + "</div>");
        container.addClass("vehicle");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        return container;
    },

    renderFull: function ($super) {
        var container = $super();
        container.addClass("vehicle-full");

        var mileage = $("<div>Mileage: " + this.mileage + " per 100km</div>");
        mileage.addClass("mileage-visualisation");
        container.append(mileage);

        var leasing = $("<div>Leasing: " + this.leasing + "</div>");
        leasing.addClass("leasing-visualisation");
        container.append(leasing);

        var typeOfVehicle = $("<div>Type of vehicle: " + this.typeOfVehicle + "</div>");
        typeOfVehicle.addClass("typeOfVehicle-visualisation");
        container.append(typeOfVehicle);

        return container;
    }
});

var Branch = Class.create({
    initialize: function (town, numberOfPotentialClients) {
        this.town = town;
        this.numberOfPotentialClients = numberOfPotentialClients;
        this.buildings = [];
    },

    numberOfWorkers: function () {
        var counter = 0;
        for (var i = 0, len = this.buildings.length; i < len; i++) {
            counter = counter + this.buildings[i].workers.length;
        }

        return counter;
    },

    numberOfMachines: function () {
        var counter = 0;
        for (var i = 0, len = this.buildings.length; i < len; i++) {
            if (this.buildings[i].machines){
                counter = counter + this.buildings[i].machines.length;
            }
        }

        return counter;
    },

    numberOfVehicles: function () {
        var counter = 0;
        for (var i = 0, len = this.buildings.length; i < len; i++) {
            if (this.buildings[i].machines){
                counter = counter + this.buildings[i].vehicles.length;
            }
        }

        return counter;
    },

    addBuilding: function (building) {
        if (!(building instanceof Building)) {
            throw new Error("Only buildings can be added to branches.");
        }

        //check if building with the same parameters is not added already
        for (var i = this.buildings.length - 1; i >= 0; i--) {
            if (this.buildings[i] == building) {
                return;
            }
        };

        this.buildings.push(building);
        if (this.container) {
            if (this.container.children("ul.list-of-buildings").length > 0) {
                $(this.container).children("ul.list-of-buildings").first().append(building.renderPartial());
            }
            else {
                var buildingsList = $("<ul></ul>");
                buildingsList.addClass("list-of-buildings");
                for (var i = 0, len = this.buildings.length; i < len; i++) {
                    var listItem = this.buildings[i].renderPartial();
                    buildingsList.append(listItem);
                }

                buildingsList.sortable();
                $(this.container).append(buildingsList);
            }
        }

        if (this.fullContainer) {
            if ($(this.fullContainer).parent().length > 0) {
                $(this.fullContainer).children(".number-container-buildings").first().html("Number of buildings: " + this.buildings.length);
                $(this.fullContainer).children(".number-container-workers").first().html("Number of workers: " + this.numberOfWorkers());
                $(this.fullContainer).children(".number-container-machines").first().html("Number of machines: " + this.numberOfMachines());
            }
        }
    },

    renderPartial: function () {
        var container = $("<div></div>");
        container.addClass("branch");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        var branchName = $("<div>Branch: " + this.town + "</div>");
        branchName.addClass("branchName");
        container.append(branchName);

        if (this.buildings.length > 0) {
            var buildingsList = $("<ul></ul>");
            buildingsList.addClass("list-of-buildings");
            for (var i = 0, len = this.buildings.length; i < len; i++) {
                var listItem = this.buildings[i].renderPartial();
                buildingsList.append(listItem);
            }

            buildingsList.sortable();
            container.append(buildingsList);
        }

        this.container = container;
        return container;
    },

    renderFull: function () {
        var container = $("<div></div>");
        container.addClass("branch-full");

        var branchName = $("<div>Branch town: " + this.town + "</div>");
        branchName.addClass("branch-name");
        container.append(branchName);

        var customers = $("<div>Potential clients: " + this.numberOfPotentialClients + "</div>");
        container.append(customers);

        var buildings = $("<div>Number of buildings: " + this.buildings.length + "</div>");
        buildings.addClass("number-container-buildings");
        container.append(buildings);

        var numberOfWorkers = $("<div>Number of workers:" + this.numberOfWorkers() + "</div>");
        numberOfWorkers.addClass("number-container-workers");
        container.append(numberOfWorkers);

        var numberOfMachines = $("<div>Number of machines:" + this.numberOfMachines() + "</div>");
        numberOfMachines.addClass("number-container-machines");
        container.append(numberOfMachines);

        var numberOfVehicles = $("<div>Number of vehicles:" + this.numberOfVehicles() + "</div>");
        numberOfMachines.addClass("number-container-vehicles");
        container.append(numberOfVehicles);

        this.fullContainer = container;
        return container;
    }
});

var Company = Class.create({
    initialize: function (name, value) {
        this.name = name;
        this.value = value;
        this.branches = [];
    },

    numberOfWorkers: function () {
        var counter = 0;
        for (var i = 0, len = this.branches.length; i < len; i++) {
            counter = counter + this.branches[i].numberOfWorkers();
        }

        return counter;
    },

    numberOfMachines: function () {
        var counter = 0;
        for (var i = 0, len = this.branches.length; i < len; i++) {
            counter = counter + this.branches[i].numberOfMachines();
        }

        return counter;
    },

    numberOfVehicles: function () {
        var counter = 0;
        for (var i = 0, len = this.branches.length; i < len; i++) {
            counter = counter + this.branches[i].numberOfVehicles();
        }

        return counter;
    },

    addBranch: function (branch) {
        if (!(branch instanceof Branch)) {
            throw new Error("Only branches can be added to companies.");
        }

        //check if building with the same parameters is not added already
        for (var i = this.branches.length - 1; i >= 0; i--) {
            if (this.branches[i] === branch) {
                return;
            }
        };

        this.branches.push(branch);
        if (this.container) {
            if (this.container.children("ul.list-of-branches").length > 0) {
                $(this.container).children("ul.list-of-branches").first().append(branch.renderPartial());
            }
            else {
                var branchesList = $("<ul></ul>");
                branchesList.addClass("list-of-branches");
                for (var i = 0, len = this.branches.length; i < len; i++) {
                    var listItem = this.branches[i].renderPartial();
                    branchesList.append(listItem);
                }

                branchesList.sortable();
                $(this.container).append(branchesList);
            }
        }

        if (this.fullContainer) {
            if ($(this.fullContainer).parent().length > 0) {
                $(this.fullContainer).children(".number-container-branches").first().html("Number of branches: " + this.branches.length);
                $(this.fullContainer).children(".number-container-workers").first().html("Number of workers: " + this.numberOfWorkers());
                $(this.fullContainer).children(".number-container-machines").first().html("Number of machines: " + this.numberOfMachines());
            }
        }
    },

    renderPartial: function () {
        var container = $("<div></div>");
        container.addClass("company");
        var self = this;
        container.on("click", function (event) {
            event.stopPropagation();
            var cont = $("#container-selected");
            if (cont.length == 0) {
                var selectedContainer = $("<div></div>");
                selectedContainer.attr("id", "container-selected");
                $(document.body).append(selectedContainer);

                var previousButton = $("<button>Previous</button>");
                previousButton.attr("id", "prev-button");
                previousButton.on("click", localStorageControl.previous);
                $(document.body).append(previousButton);

                var nextButton = $("<button>Next</button>");
                nextButton.attr("id", "next-button");
                nextButton.on("click", localStorageControl.next);
                $(document.body).append(nextButton);
            }

            var container = $("#container-selected").first();
            container.html("");
            container.append(self.renderFull());
            localStorageControl.add();
            createTwitterButton();
        });

        var companyName = $("<div>Company: " + this.name + "</div>");
        companyName.addClass("companyName");
        container.append(companyName);

        if (this.branches.length > 0) {
            var branchesList = $("<ul></ul>");
            branchesList.addClass("list-of-branches");
            for (var i = 0, len = this.branches.length; i < len; i++) {
                var listItem = this.branches[i].renderPartial();
                branchesList.append(listItem);
            }

            branchesList.sortable();
            container.append(branchesList);
        }

        this.container = container;
        return container;
    },

    renderFull: function () {
        var container = $("<div></div>");
        container.addClass("company-full");

        var companyName = $("<div>Company: " + this.name + "</div>");
        companyName.addClass("company-name");
        container.append(companyName);

        var value = $("<div>Market value: " + this.value + "</div>");
        container.append(value);

        var branches = $("<div>Number of branches: " + this.branches.length + "</div>");
        branches.addClass("number-container-branches");
        container.append(branches);

        var numberOfWorkers = $("<div>Number of workers: " + this.numberOfWorkers() + "</div>");
        numberOfWorkers.addClass("number-container-workers");
        container.append(numberOfWorkers);

        var numberOfMachines = $("<div>Number of machines: " + this.numberOfMachines() + "</div>");
        numberOfMachines.addClass("number-container-machines");
        container.append(numberOfMachines);

        var numberOfVehicles = $("<div>Number of vehicles: " + this.numberOfVehicles() + "</div>");
        numberOfMachines.addClass("number-container-vehicles");
        container.append(numberOfVehicles);

        this.fullContainer = container;
        return container;
    }
});

// Can be implemented later for generating the buttons and form dynamically.

//jQuery.fn.extend({
//    createDialogForm: function (className) {
//        var div = document.createElement("div");
//        div.className = 'dialog-form-' + className.toLowerCase();
//        $div = $(div);
//        $div.attr("display", "none");
//        $div.append('<p class="validateTips">All form fields are required.</p>');
//        $form = $('<form></form>');
//        $fieldSet = $('<fieldset></fieldset>');
//        for (var i = 1; i < arguments.length; i++) {
//            $fieldSet.append('<label for="' + arguments[i].toLowerCase() + '">' + arguments[i] + '</label><input type="text" name="' + arguments[i].toLowerCase() + '" id="' + arguments[i].toLowerCase() + '-' + className.toLowerCase() + '" class="text ui-widget-content ui-corner-all" />');
//        }
//        $form.append($fieldSet);
//        $div.append($form);
//        $(this).append($div);
//    },

//    createButton: function (className) {
//        $(this).append('<button id="create-' + className.toLowerCase() + '">Create new ' + className.toLowerCase() + '</button>');
//    }
//});

//$('body').createButton("Vehicle");
//$('body').createDialogForm("Vehicle", "Years", "Guarantee", "AmortizationPeriod", "Mileage", "Leasing", "TypeOfVehicle");

$(function () {
    var fnameWorker = $("#fname-Worker"),
      lnameWorker = $("#lname-Worker"),
      ageWorker = $("#age-Worker"),
      workExperienceWorker = $("#workExperience-Worker"),

      fnameExpert = $("#fname-Expert"),
      lnameExpert = $("#lname-Expert"),
      ageExpert = $("#age-Expert"),
      workExperienceExpert = $("#workExperience-Expert"),
      degreeExpert = $("#degree-Expert"),

      fnameManager = $("#fname-Manager"),
      lnameManager = $("#lname-Manager"),
      ageManager = $("#age-Manager"),
      workExperienceManager = $("#workExperience-Manager"),
      degreeManager = $("#degree-Manager"),

      nameBuilding = $("#name-building"),
      areaBuilding = $("#area-building"),
      retailPriceBuilding = $("#retailPrice-building"),
      rentedBuilding = $("#rented-building"),
      yearlyRentBuilding = $("#yearlyRent-building"),

      nameAdministrativeBuilding = $("#name-administrative-building"),
      areaAdministrativeBuilding = $("#area-administrative-building"),
      retailPriceAdministrativeBuilding = $("#retailPrice-administrative-building"),
      rentedAdministrativeBuilding = $("#rented-administrative-building"),
      yearlyRentAdministrativeBuilding = $("#yearlyRent-administrative-building"),

      nameProductionBuilding = $("#name-production-building"),
      areaProductionBuilding = $("#area-production-building"),
      retailPriceProductionBuilding = $("#retailPrice-production-building"),
      rentedProductionBuilding = $("#rented-production-building"),
      yearlyRentProductionBuilding = $("#yearlyRent-production-building"),

      yearsMachine = $("#years-machine"),
      guaranteeMachine = $("#guarantee-machine"),
      amortizationPeriodMachine = $("#amortizationPeriod-machine"),

      yearsServer = $("#years-server"),
      guaranteeServer = $("#guarantee-server"),
      amortizationPeriodServer = $("#amortizationPeriod-server"),
      priceServer = $("#price-server"),
      osServer = $("#os-server"),
      usageServer = $("#usage-server"),

      yearsVehicle = $("#years-vehicle"),
      guaranteeVehicle = $("#guarantee-vehicle"),
      amortizationPeriodVehicle = $("#amortizationPeriod-vehicle"),
      mileageVehicle = $("#mileage-vehicle"),
      leasingVehicle = $("#leasing-vehicle"),
      typeOfVehicleVehicle = $("#typeOfVehicle-vehicle"),

      townBranch = $("#town-branch"),
      numberOfPotentialClientsBranch = $("#numberOfPotentialClients-branch"),

      allFields = $([]),
      tips = $(".validateTips");

    function updateTips(t) {
        tips
          .text(t)
          .addClass("ui-state-highlight");
        setTimeout(function () {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);
    }

    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
              min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }

    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }

    $("#dialog-form-worker").dialog({
        autoOpen: false,
        height: 400,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(fnameWorker, "first name", 3, 16);
                bValid = bValid && checkLength(lnameWorker, "last name", 3, 16);
                bValid = bValid && checkLength(ageWorker, "age", 1, 2);
                bValid = bValid && checkLength(workExperienceWorker, "work experience", 1, 2);

                bValid = bValid && checkRegexp(fnameWorker, /^[A-Z]([a-z])+$/i, "First name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(lnameWorker, /^[A-Z]([a-z])+$/i, "Last name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(ageWorker, /^[1-9]([0-9])+$/i, "Age should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(workExperienceWorker, /^[0-4]([0-9])+$/i, "Work experience should be between 0-49 years");

                if (bValid) {
                    var worker = new Worker(fnameWorker.val(), lnameWorker.val(), ageWorker.val(), workExperienceWorker.val());
                    $(document.body).append(worker.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-expert").dialog({
        autoOpen: false,
        height: 500,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(fnameExpert, "first name", 3, 16);
                bValid = bValid && checkLength(lnameExpert, "last name", 3, 16);
                bValid = bValid && checkLength(ageExpert, "age", 1, 2);
                bValid = bValid && checkLength(workExperienceExpert, "work experience", 1, 2);
                bValid = bValid && checkLength(degreeExpert, "degree", 1, 30);

                bValid = bValid && checkRegexp(fnameExpert, /^[A-Z]([a-z])+$/i, "First name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(lnameExpert, /^[A-Z]([a-z])+$/i, "Last name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(ageExpert, /^[1-9]([0-9])+$/i, "Age should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(workExperienceExpert, /^([0-9])+$/i, "Work experience should consist of 0-9.");
                bValid = bValid && checkRegexp(degreeExpert, /^[A-Z]([a-z0-9])+$/i, "Degree should consist of a-z and 0-9 beginning with A-Z.");

                if (bValid) {
                    var expert = new Expert(fnameExpert.val(), lnameExpert.val(), ageExpert.val(), workExperienceExpert.val(), degreeExpert.val());
                    $(document.body).append(expert.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-manager").dialog({
        autoOpen: false,
        height: 500,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(fnameManager, "first name", 3, 16);
                bValid = bValid && checkLength(lnameManager, "last name", 3, 16);
                bValid = bValid && checkLength(ageManager, "age", 1, 2);
                bValid = bValid && checkLength(workExperienceManager, "work experience", 1, 2);
                bValid = bValid && checkLength(degreeManager, "degree", 1, 30);

                bValid = bValid && checkRegexp(fnameManager, /^[A-Z]([a-z])+$/i, "First name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(lnameManager, /^[A-Z]([a-z])+$/i, "Last name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(ageManager, /^[1-9]([0-9])+$/i, "Age should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(workExperienceManager, /^([0-9])+$/i, "Work experience should consist of 0-9.");
                bValid = bValid && checkRegexp(degreeManager, /^[A-Z]([a-z0-9])+$/i, "Degree should consist of a-z and 0-9 beginning with A-Z.");

                if (bValid) {
                    var manager = new Manager(fnameManager.val(), lnameManager.val(), ageManager.val(), workExperienceManager.val(), degreeManager.val());
                    $(document.body).append(manager.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-building").dialog({
        autoOpen: false,
        height: 500,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(nameBuilding, "building name", 3, 16);
                bValid = bValid && checkLength(areaBuilding, "area", 3, 16);
                bValid = bValid && checkLength(retailPriceBuilding, "retail price", 1, 10);
                bValid = bValid && checkLength(rentedBuilding, "rented", 1, 3);
                bValid = bValid && checkLength(yearlyRentBuilding, "yearly rent", 1, 10);

                bValid = bValid && checkRegexp(nameBuilding, /^[A-Z]([a-z])+$/i, "Building name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(areaBuilding, /^[A-Z]([a-z])+$/i, "Area name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(retailPriceBuilding, /^[1-9]([0-9])+$/i, "Retail price should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(rentedBuilding, /^[a-z]([a-z])+$/i, "Rented should consist of 0-9.");
                bValid = bValid && checkRegexp(yearlyRentBuilding, /^[1-9]([0-9])+$/i, "Yearly rent should consist of 0-9 beginning with 1-9.");

                if (bValid) {
                    var building = new Building(nameBuilding.val(), areaBuilding.val(), retailPriceBuilding.val(), rentedBuilding.val(), yearlyRentBuilding.val());
                    $(document.body).append(building.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-administrative-building").dialog({
        autoOpen: false,
        height: 500,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(nameAdministrativeBuilding, "building name", 3, 16);
                bValid = bValid && checkLength(areaAdministrativeBuilding, "area", 3, 16);
                bValid = bValid && checkLength(retailPriceAdministrativeBuilding, "retail price", 1, 10);
                bValid = bValid && checkLength(rentedAdministrativeBuilding, "rented", 1, 3);
                bValid = bValid && checkLength(yearlyRentAdministrativeBuilding, "yearly rent", 1, 10);

                bValid = bValid && checkRegexp(nameAdministrativeBuilding, /^[A-Z]([a-z])+$/i, "Building name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(areaAdministrativeBuilding, /^[A-Z]([a-z])+$/i, "Area name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(retailPriceAdministrativeBuilding, /^[1-9]([0-9])+$/i, "Retail price should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(rentedAdministrativeBuilding, /^[a-z]([a-z])+$/i, "Rented should consist of 0-9.");
                bValid = bValid && checkRegexp(yearlyRentAdministrativeBuilding, /^[1-9]([0-9])+$/i, "Yearly rent should consist of 0-9 beginning with 1-9.");

                if (bValid) {
                    var administrativeBuilding = new AdministrativeBuilding(nameAdministrativeBuilding.val(), areaAdministrativeBuilding.val(), retailPriceAdministrativeBuilding.val(), rentedAdministrativeBuilding.val(), yearlyRentAdministrativeBuilding.val());
                    $(document.body).append(administrativeBuilding.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-production-building").dialog({
        autoOpen: false,
        height: 500,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(nameProductionBuilding, "building name", 3, 16);
                bValid = bValid && checkLength(areaProductionBuilding, "area", 3, 16);
                bValid = bValid && checkLength(retailPriceProductionBuilding, "retail price", 1, 10);
                bValid = bValid && checkLength(rentedProductionBuilding, "rented", 1, 3);
                bValid = bValid && checkLength(yearlyRentProductionBuilding, "yearly rent", 1, 10);

                bValid = bValid && checkRegexp(nameProductionBuilding, /^[A-Z]([a-z])+$/i, "Building name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(areaProductionBuilding, /^[A-Z]([a-z])+$/i, "Area name should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(retailPriceProductionBuilding, /^[1-9]([0-9])+$/i, "Retail price should consist of 0-9 beginning with 1-9.");
                bValid = bValid && checkRegexp(rentedProductionBuilding, /^[a-z]([a-z])+$/i, "Rented should consist of 0-9.");
                bValid = bValid && checkRegexp(yearlyRentProductionBuilding, /^[1-9]([0-9])+$/i, "Yearly rent should consist of 0-9 beginning with 1-9.");

                if (bValid) {
                    var productionBuilding = new ProductionBuilding(nameProductionBuilding.val(), areaProductionBuilding.val(), retailPriceProductionBuilding.val(), rentedProductionBuilding.val(), yearlyRentProductionBuilding.val());
                    $(document.body).append(productionBuilding.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-machine").dialog({
        autoOpen: false,
        height: 350,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(yearsMachine, "years", 1, 2);
                bValid = bValid && checkLength(guaranteeMachine, "guarantee", 1, 2);
                bValid = bValid && checkLength(amortizationPeriodMachine, "amortization period", 1, 2);

                bValid = bValid && checkRegexp(yearsMachine, /^[0-9]([0-9])+$/i, "Years should consist of 0-9.");
                bValid = bValid && checkRegexp(guaranteeMachine, /^[0-9]([0-9])+$/i, "Guarantee should consist of 0-9.");
                bValid = bValid && checkRegexp(amortizationPeriodMachine, /^[0-9]([0-9])+$/i, "Amortization period should consist of 0-9.");

                if (bValid) {
                    var machine = new Machine(yearsMachine.val(), guaranteeMachine.val(), amortizationPeriodMachine.val());
                    $(document.body).append(machine.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-server").dialog({
        autoOpen: false,
        height: 600,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(yearsServer, "years", 1, 2);
                bValid = bValid && checkLength(guaranteeServer, "guarantee", 1, 2);
                bValid = bValid && checkLength(amortizationPeriodServer, "amortization period", 1, 2);
                bValid = bValid && checkLength(priceServer, "price", 0, 16);
                bValid = bValid && checkLength(osServer, "operating system", 1, 16);
                bValid = bValid && checkLength(usageServer, "usage", 1, 16);

                bValid = bValid && checkRegexp(yearsServer, /^[0-9]([0-9])+$/i, "Years should consist of 0-9.");
                bValid = bValid && checkRegexp(guaranteeServer, /^[0-9]([0-9])+$/i, "Guarantee should consist of 0-9.");
                bValid = bValid && checkRegexp(amortizationPeriodServer, /^[0-9]([0-9])+$/i, "Amortization period should consist of 0-9.");
                bValid = bValid && checkRegexp(priceServer, /^[0-9]([0-9])+$/i, "Price should consist of 0-9.");
                bValid = bValid && checkRegexp(osServer, /^[A-Z]([a-z])+$/i, "Operating system should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(usageServer, /^[A-Z]([a-z])+$/i, "Usage should consist of a-z beginning with A-Z.");

                if (bValid) {
                    var server = new Server(yearsServer.val(), guaranteeServer.val(), amortizationPeriodServer.val(), priceServer.val(), osServer.val(), usageServer.val());
                    $(document.body).append(server.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-vehicle").dialog({
        autoOpen: false,
        height: 600,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(yearsVehicle, "years", 1, 2);
                bValid = bValid && checkLength(guaranteeVehicle, "guarantee", 1, 2);
                bValid = bValid && checkLength(amortizationPeriodVehicle, "amortization period", 1, 2);
                bValid = bValid && checkLength(mileageVehicle, "mileage", 0, 6);
                bValid = bValid && checkLength(leasingVehicle, "leasing", 1, 2);
                bValid = bValid && checkLength(typeOfVehicleVehicle, "type of vehicle", 1, 16);

                bValid = bValid && checkRegexp(yearsVehicle, /^[0-9]([0-9])+$/i, "Years should consist of 0-9.");
                bValid = bValid && checkRegexp(guaranteeVehicle, /^[0-9]([0-9])+$/i, "Guarantee should consist of 0-9.");
                bValid = bValid && checkRegexp(amortizationPeriodVehicle, /^[0-9]([0-9])+$/i, "Amortization period should consist of 0-9.");
                bValid = bValid && checkRegexp(mileageVehicle, /^[0-9]([0-9])+$/i, "Mileage should consist of 0-9.");
                bValid = bValid && checkRegexp(leasingVehicle, /^[0-9]([0-9])+$/i, "Leasing should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(typeOfVehicleVehicle, /^[A-Z]([a-z])+$/i, "Type of vehicle consist of a-z beginning with A-Z.");

                if (bValid) {
                    var vehicle = new Vehicle(yearsVehicle.val(), guaranteeVehicle.val(), amortizationPeriodVehicle.val(), mileageVehicle.val(), leasingVehicle.val(), typeOfVehicleVehicle.val());
                    $(document.body).append(vehicle.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#dialog-form-branch").dialog({
        autoOpen: false,
        height: 350,
        width: 400,
        modal: true,
        buttons: {
            "Create": function () {
                var bValid = true;
                allFields.removeClass("ui-state-error");

                bValid = bValid && checkLength(townBranch, "town", 1, 16);
                bValid = bValid && checkLength(numberOfPotentialClientsBranch, "number of potential clients", 1, 16);

                bValid = bValid && checkRegexp(townBranch, /^[A-Z]([a-z])+$/i, "Town should consist of a-z beginning with A-Z.");
                bValid = bValid && checkRegexp(numberOfPotentialClientsBranch, /^[1-9]([0-9])+$/i, "Number of potential clients should consist of 0-9 beginning with 1-9.");

                if (bValid) {
                    var branch = new Branch(townBranch.val(), numberOfPotentialClientsBranch.val());
                    $(document.body).append(branch.renderPartial());
                    $(this).dialog("close");
                }
            },
            Cancel: function () {
                console.log(allFields);
                $(this).dialog("close");
            }
        },
        close: function () {
            allFields.val("").removeClass("ui-state-error");
        }
    });

    $("#create-worker")
      .button()
      .click(function () {
          allFields = $([]).add(fnameWorker).add(lnameWorker).add(ageWorker).add(workExperienceWorker);
          $("#dialog-form-worker").dialog("open");
      });

    $("#create-expert")
      .button()
      .click(function () {
          allFields = $([]).add(fnameExpert).add(lnameExpert).add(ageExpert).add(workExperienceExpert).add(degreeExpert);
          $("#dialog-form-expert").dialog("open");
      });

    $("#create-manager")
      .button()
      .click(function () {
          allFields = $([]).add(fnameManager).add(lnameManager).add(ageManager).add(workExperienceManager).add(degreeManager);
          $("#dialog-form-manager").dialog("open");
      });

    $("#create-building")
      .button()
      .click(function () {
          allFields = $([]).add(nameBuilding).add(areaBuilding).add(retailPriceBuilding).add(rentedBuilding).add(yearlyRentBuilding);
          $("#dialog-form-building").dialog("open");
      });

    $("#create-administrative-building")
      .button()
      .click(function () {
          allFields = $([]).add(nameAdministrativeBuilding).add(areaAdministrativeBuilding).add(retailPriceAdministrativeBuilding).add(rentedAdministrativeBuilding).add(yearlyRentAdministrativeBuilding);
          $("#dialog-form-administrative-building").dialog("open");
      });

    $("#create-production-building")
      .button()
      .click(function () {
          allFields = $([]).add(nameProductionBuilding).add(areaProductionBuilding).add(retailPriceProductionBuilding).add(rentedProductionBuilding).add(yearlyRentProductionBuilding);
          $("#dialog-form-production-building").dialog("open");
      });

    $("#create-machine")
      .button()
      .click(function () {
          allFields = $([]).add(yearsMachine).add(guaranteeMachine).add(amortizationPeriodMachine);
          $("#dialog-form-machine").dialog("open");
      });

    $("#create-server")
      .button()
      .click(function () {
          allFields = $([]).add(yearsServer).add(guaranteeServer).add(amortizationPeriodServer).add(priceServer).add(osServer).add(usageServer);
          $("#dialog-form-server").dialog("open");
      });

    $("#create-vehicle")
      .button()
      .click(function () {
          allFields = $([]).add(yearsVehicle).add(guaranteeVehicle).add(amortizationPeriodVehicle).add(mileageVehicle).add(leasingVehicle).add(typeOfVehicleVehicle);
          $("#dialog-form-vehicle").dialog("open");
      });

    $("#create-branch")
      .button()
      .click(function () {
          allFields = $([]).add(townBranch).add(numberOfPotentialClientsBranch);
          $("#dialog-form-branch").dialog("open");
      });

});